package com.bdzl.pub.util.param;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bdzl.pub.common.Tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BuildTree {

    public static <T> Tree<T> build(List<Tree<T>> nodes) {
        if (nodes == null||nodes.isEmpty())  return null;
        List<Tree<T>> topNodes = new ArrayList<>();
		Map<String,Tree<T>> hash = new HashMap<>();
		//将数组转为Object的形式，key为数组中的id
		nodes.forEach(json->hash.put(json.getId(),json));
        for (Tree<T> children : nodes) {
			Tree<T> hashVP = hash.get(children.getParentId());
			if (hashVP != null) {
				if (hashVP.getChildren() != null) {
					hashVP.getChildren().add(children);
				} else {
					List<Tree<T>> ch = new ArrayList<>();
					ch.add(children);
					hashVP.setChildren( ch);
				}
			} else {
				topNodes.add(children);
			}
        }

        Tree<T> root = new Tree<>();
        if (topNodes.size() == 1) {
            root = topNodes.get(0);
        } else {
            root.setId("-1");
            root.setParentId("");
            root.setHasParent(false);
            root.setHasChildren(true);
            root.setSelected(true);
            root.setChildren(topNodes);
            root.setText("顶级节点");
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            root.setState(state);
        }

        return root;
    }

	public static <T> List<Tree<T>> buildList(List<Tree<T>> nodes, String idParam) {
		if (nodes == null) {
			return null;
		}
		List<Tree<T>> topNodes = new ArrayList<>();

		for (Tree<T> children : nodes) {

			String pid = children.getParentId();
			if (pid == null || idParam.equals(pid)) {
				topNodes.add(children);

				continue;
			}

			for (Tree<T> parent : nodes) {
				String id = parent.getId();
				if (id != null && id.equals(pid)) {
					parent.getChildren().add(children);
					children.setHasParent(true);
					parent.setHasChildren(true);

					continue;
				}
			}

		}
		return topNodes;
	}

	/**
	 * 将list数据转换树形结构，使用JSON可以方便的将对象转换为实体类，Map和其他泛型
	 * @param arr	数据可以有List<Map>转来
	 * @param id	id为arr数据中key的名称 就是唯一标识
	 * @param pid	父ID pid为arr数据中key的名称
	 * @param child	子节点存放的名成 ()
	 * @return
	 */
	public static JSONArray listToTree(JSONArray arr, String id, String pid, String child) {
		JSONArray r = new JSONArray();
		JSONObject hash = new JSONObject();
		//将数组转为Object的形式，key为数组中的id
		arr.forEach(json->hash.put(((JSONObject)json).getString(id),json));
		//遍历结果集
		for (int j = 0; j < arr.size(); j++) {
			//单条记录
			JSONObject aVal = (JSONObject) arr.get(j);
			//在hash中取出key为单条记录中pid的值
			JSONObject hashVP = (JSONObject) hash.get(aVal.getString(pid));
			//如果记录的pid存在，则说明它有父节点，将她添加到孩子节点的集合中
			if (hashVP != null) {
				//检查是否有child属性
				if (hashVP.get(child) != null) {
					JSONArray ch = (JSONArray) hashVP.get(child);
					ch.add(aVal);
					hashVP.put(child, ch);
				} else {
					JSONArray ch = new JSONArray();
					ch.add(aVal);
					hashVP.put(child, ch);
				}
			} else {
				r.add(aVal);
			}
		}
		return r;
	}
}